package OracleDB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleDB {
	


private Connection conn;
public void dbconn(){
		
			//1) 오라클 드라이버 로딩
			String url="jdbc:oracle:thin:@127.0.0.1:1521";// 현장에서는 달라질수잇음..(@ 뒷 부분)
			String user="hr";
			String password = "hr";
			try{
				Class.forName("oracle.jdbc.driver.OracleDriver");
				System.out.println("드라이버가 로딩됨...");
				
				
				
				
			//2) 오라클 DB와 연결
				Connection conn = DriverManager.getConnection(url, user, password);
				
				System.out.println("DB가 연결됨..");
			
			
	}catch(Exception ex){
		ex.printStackTrace();
	}
}	
	
	
	
	
	
	public OracleDB(){
		//1) 오라클 드라이버 로딩
		String url="jdbc:oracle:thin:@127.0.0.1:1521";
		String user="hr";
		String password = "hr";
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("드라이버가 로딩됨...");
			
			
			
			
		//2) 오라클 DB와 연결
			Connection conn = DriverManager.getConnection(url, user, password);
			
			System.out.println("DB가 연결됨..");
			
	
			
			
			
	//		------------------------------------------------------------------------------------ jsp 할떄도 똑같댄다.기억하렴
			
			
		//3) Statement 생성(DQL,DML)
			String sql = "select * from emp";
			Statement stmt = conn.createStatement();
			
			
			
		//4) statement 실행
			ResultSet rs= stmt.executeQuery(sql);
			
			
		//5) 실행결과 출력
			while(rs.next()){
				System.out.println(rs.getString("last_name") + ","+ rs.getString("email"));
			}
			
			rs.close();
			conn.close();   // db 안닫아주면 리소스 소모가 된대..
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	public void prepareCon(){
		try{
			
			//3)statement 생성
			String sql = "select *from jobs where min_salary>?";
			PreparedStatement pstmt = conn.prepareStatement(sql); 	
			
							pstmt.setInt(1,5000);
			//where job_title like %ger%"       ,ger 부분이 ? 부분이다
			//4) statement 실행
			ResultSet rs = pstmt.executeQuery(); // 반환타입이 resultSet 이란다
			
			//5) 실행결과 출력
			while(rs.next()){
			/*	System.out.println(     // field 명 대신 getString(숫자를 넣음)으로 순서를 기입가능함..그런데 안쓴댄다
						rs.getString("job_id")+ ","+
						rs.getString("job_title")+","+ 
						rs.getInt("min_salary")+","+
						rs.getInt("max_salary"));
*/
			Jobs j = new Jobs();
			j.setjob_id(rs.getString("job_id"));
			j.setjob_title(rs.getString("job_title"));
			j.setMin_salary(rs.getInt("min_salary"));
			j.setMax_salary(rs.getInt("max_salary"));
			
			v.add(j);
			}
		}catch(Exception ex){
			
		}
		
	}
	
	public Connection getConn() {
		return conn;
	}





	public void setConn(Connection conn) {
		this.conn = conn;
	}





	public static void main(String[] args) {
		
		new OracleDB().prepareCon();
		
	}

}
